package com.kfy.observerdemo.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: ObserverDemo
 * @Package: com.kfy.observerdemo
 * @Author: K.F.Y
 * @Date: 2021/5/8
 */
public class NewsSubject implements Subject {

    public String name = "天气预报";
    private String content;
    private List<Observer> observers = new ArrayList<>();

    public NewsSubject(String name){
        this.name = name;
    }

    @Override
    public String getName( ) {
        return name;
    }

    @Override
    public void setContent(String content) {
        this.content = content;
        publish();
    }

    @Override
    public String getContent() {
        return this.content;
    }

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void publish() {
        for (Observer observer : observers){
            observer.update(this);
        }
    }
}
